package com.clever.mall.controller;

import com.clever.mall.commons.dto.OrderDto;
import com.clever.mall.commons.dto.PayDto;
import com.clever.mall.commons.result.BaseResult;
import com.clever.mall.service.OrderService;
import com.clever.mall.vo.IsOrderVo;
import com.clever.mall.vo.OrderVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: project-item
 * @author: hzx
 * @since: JDK 1.8
 * @create: 2020-11-09 15:09
 **/

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    OrderService service;

    /**
     * 通过用户id查询用户所有订单
     * @param uid
     * @return
     */
    @GetMapping("/all")
    public BaseResult<List<OrderVo>> orderAll(int uid) {
        return BaseResult.success(service.selectOrderAll(uid));
    }

    /**
     * 通过用户id和状态码查询用户订单
     * @param uid
     * @return
     */
    @GetMapping("/status")
    public BaseResult<List<OrderVo>> orderStatus(int uid ,String status) {
        return BaseResult.success(service.selectOrderByStatus(uid,status));
    }

    /**
     * 通过订单id查询用户订单详情
     * @param oid
     * @return
     */
    @GetMapping("/id")
    public BaseResult<OrderVo> orderById(int oid) {
        return BaseResult.success(service.selectOrderDetailsByOrderId(oid));
    }

    /**
     * 添加单个订单
     * @param orderDto
     * @return
     */
    @PostMapping("/add")
    public BaseResult<Integer> addOrder(@RequestBody OrderDto orderDto) {
        return BaseResult.success(service.insertOrder(orderDto));
    }

    @PostMapping("/addList")
    public BaseResult<Integer> addOrderList(@RequestBody List<OrderDto> orderDtoList) {
        return BaseResult.success(service.insertOrderList(orderDtoList));
    }

    /**
     * 通过订单id删除订单
     * @param oid
     * @return
     */
    @PostMapping("/del")
    public BaseResult<Integer> delOrder(int oid) {
        return BaseResult.success(service.deleteOrderByOrderId(oid));
    }

    @PostMapping("/delList")
    public BaseResult<Integer> delOrderList(@RequestBody List<Integer> oidList) {
        return BaseResult.success(service.deleteOrderListByOrderListId(oidList));
    }

    /**
     * 修改订单状态
     * @param orderId
     * @param status
     * @return
     */
    @PostMapping("/updStatus")
    public BaseResult<Integer> updateOrderStatus(int orderId, int status) {
        return BaseResult.success(service.updateOrderStatus(orderId, status));
    }

    /**
     * 修改订单退款状态
     * @param orderId
     * @param refundStatus
     * @return
     */
    @PostMapping("/updRefundStatus")
    public BaseResult<Integer> updateOrderRefundStatus(int orderId, int refundStatus) {
        return BaseResult.success(service.updateOrderRefundStatus(orderId, refundStatus));
    }

    /**
     * 确认订单数据
     * @param uid
     * @param pid
     * @return
     */
    @GetMapping("/isOrder")
    public BaseResult<IsOrderVo>  isOrder(int uid, int pid) {
        return BaseResult.success(service.selectIsOrderVoByUserIdAndProductId(uid, pid));
    }

    /**
     * 付款
     * @param payDto
     */
    @PostMapping("/pay")
    public BaseResult<Boolean> payApi(@RequestBody PayDto payDto) {
        return BaseResult.success(service.pay(payDto));
    }
}
